package com.puao.service.impl;

import com.puao.bean.Ball;
import com.puao.bean.BallIn;
import com.puao.dao.BallInMapper;
import com.puao.dao.BallMapper;
import com.puao.service.IAnalysisService;
import com.puao.util.FpGrowthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;
import java.util.Map.Entry;

@Component("analysisService")
public class AnalysisServiceImpl implements IAnalysisService {

    @Autowired
    private BallMapper ballMapper;

    @Autowired
    private BallInMapper moneyBallMapper;


    @Override
    public List<Entry<String,List<String>>> getCount(Integer begin, Integer end) {
        List<BallIn> moneyBall = moneyBallMapper.selectAll(begin, end);
        Ball ball = ballMapper.countByPeriod(begin, end);
        //List<String> ballList = getBallArray(ball);
        Map<String,Integer> ballMap =  getBallMap(ball);
        Map<String,List<String>> map = new HashMap<>();
        for (BallIn in : moneyBall) {
            List<String> list = new ArrayList<>();
            list.add("r"+in.getRed_01());
            list.add("r"+in.getRed_02());
            list.add("r"+in.getRed_03());
            list.add("r"+in.getRed_04());
            list.add("r"+in.getRed_05());
            list.add("b"+in.getBlue_01());
            list.add("b"+in.getBlue_02());

            //list排序
            list.sort(new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return  ballMap.get(o2).compareTo(ballMap.get(o1));
                }
            });
            map.put(in.getPeriod().toString(),list);
        }


        //map排序,按照list数据最大的排在最前面
        List<Map.Entry<String,List<String>>> mapList = new ArrayList<>();
        mapList.addAll(map.entrySet());



        Collections.sort(mapList, new Comparator<Map.Entry<String, List<String>>>() {
            @Override
            public int compare(Map.Entry<String, List<String>> o1, Map.Entry<String, List<String>> o2) {
                String key1 = o1.getValue().get(0);
                String key2 = o2.getValue().get(0);
                return ballMap.get(key2).compareTo( ballMap.get(key1));
            }
        });
        return mapList;

    }

    @Override
    public Map<String,Integer> analysis(List<Entry<String, List<String>>> listEntry) {
        //构造linkedList
       List<List<String>> records = new ArrayList<>();
        for(Entry<String,List<String>> entry :listEntry){
            List<String> listTemp = entry.getValue();
            List<String> list = new LinkedList<>();
            list.addAll(listTemp);

            records.add(list);
        }

        Map<String,Integer> result = new HashMap<>();
        //调用fp-growth
        FpGrowthUtils fp = new FpGrowthUtils(60,result);
        fp.FPGrowth(records,null);
        result = fp.getResult();
        TreeMap<String,Integer> treeMap = new TreeMap<>();
        treeMap.putAll(result);
        return treeMap;
    }

    /**
     * 获取统计的map集合
     * ["r1":45,"r2":55.....]
     * @param ball
     * @return
     */
    public  Map<String,Integer> getBallMap(Ball ball){
        Map<String,Integer> map = new HashMap<>();
        try {
            Class clazz = Ball.class;
            Field[] fields = clazz.getDeclaredFields();
            for(Field field : fields){
                field.setAccessible(true);
                String name = field.getName();
                if(name.equals("period")){
                    continue;
                }
                Object obj  = field.get(ball);
                String str = "";
                if(name.contains("red")){
                    str = name.substring(4,6);
                }else{
                    str = name.substring(5,7);
                }
                String key = name.substring(0,1) + Integer.valueOf(str);
                map.put(key,Integer.valueOf(obj.toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public boolean isBeforeAppear(BallIn ball) {

        Integer count = moneyBallMapper.theSameOne(ball);
        if(Objects.nonNull(count) && count > 0){
            return true;
        }
        return false;
    }


    @Override
    public List<Map<String, Object>> red01(Integer begin, Integer end) {
        return moneyBallMapper.red01(begin, end);
    }

    @Override
    public List<Map<String, Object>> red02(Integer begin, Integer end) {
        return moneyBallMapper.red02(begin, end);
    }

    @Override
    public List<Map<String, Object>> red03(Integer begin, Integer end) {
        return moneyBallMapper.red03(begin, end);
    }

    @Override
    public List<Map<String, Object>> red04(Integer begin, Integer end) {
        return moneyBallMapper.red04(begin, end);
    }

    @Override
    public List<Map<String, Object>> red05(Integer begin, Integer end) {
        return moneyBallMapper.red05(begin, end);
    }

    @Override
    public List<Map<String, Object>> blue01(Integer begin, Integer end) {
        return moneyBallMapper.blue01(begin, end);
    }

    @Override
    public List<Map<String, Object>> blue02(Integer begin, Integer end) {
        return moneyBallMapper.blue02(begin, end);
    }
}
